"use strict";
cc._RF.push(module, '8bf5bgB/tBFFKc/sGVcbyn+', 'WxUtil');
// scripts/WxUtil.ts

Object.defineProperty(exports, "__esModule", { value: true });
var Core_1 = require("./Core");
var wx = window['wx'];
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var WxUtil = /** @class */ (function (_super) {
    __extends(WxUtil, _super);
    function WxUtil() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    WxUtil_1 = WxUtil;
    WxUtil.share = function (callback) {
        if (callback === void 0) { callback = null; }
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.shareAppMessage({
                title: '这关太难了，快来帮帮我！',
                imageUrl: cc.loader.getRes('icon.png'),
                success: function () {
                    if (callback) {
                        callback();
                    }
                },
                fail: function () {
                }
            });
        }
    };
    WxUtil.onShow = function (callback) {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.onShow(function () {
                if (callback) {
                    callback();
                }
            });
        }
    };
    WxUtil.showShareMenu = function () {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.showShareMenu({
                withShareTicket: true
            });
            wx.onShareAppMessage(function () {
                return {
                    title: '这关太难了，快来帮帮我！',
                    imageUrl: 'icon.png'
                };
            });
        }
    };
    WxUtil.jump = function (appid) {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.navigateToMiniProgram({
                appId: appid,
                path: '',
                extraData: null,
                envVersion: 'release',
                success: function (res) {
                    console.log('跳转成功');
                }
            });
        }
    };
    WxUtil.showTip = function (value) {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            wx.showToast({ title: value, icon: 'none', duration: 1500 });
        }
    };
    WxUtil.destroyBanner = function () {
        if (WxUtil_1.bannerAd != null) {
            WxUtil_1.bannerAd.destroy();
            WxUtil_1.bannerAd = null;
        }
    };
    WxUtil.showBanner = function () {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            var isIPX = false;
            wx.getSystemInfo({
                success: function (res) {
                    if (res.model.search('iPhone X') != -1) {
                        isIPX = true;
                    }
                }
            });
            var offsetY_1 = 0;
            if (isIPX) {
                offsetY_1 = -30;
                //  return
            }
            var winSize_1 = wx.getSystemInfoSync();
            var bannerHeight = 80;
            var bannerWidth = 300;
            var bannerAd_1 = wx.createBannerAd({
                adUnitId: WxUtil_1.BANNER_ID, adIntervals: 30,
                style: {
                    left: (winSize_1.windowWidth - bannerWidth) * 0.5,
                    top: winSize_1.windowHeight - bannerHeight + offsetY_1,
                    width: bannerWidth
                }
            });
            WxUtil_1.bannerAd = bannerAd_1;
            WxUtil_1.bannerAd.show();
            //微信缩放后得到banner的真实高度，从新设置banner的top 属性
            bannerAd_1.onResize(function (res) {
                bannerAd_1.style.top = winSize_1.windowHeight - bannerAd_1.style.realHeight + offsetY_1;
            });
            // bannerAd.onError(err => {
            //     // 进行适当的提示
            //     console.log("banner显示", err);
            // })
        }
    };
    WxUtil.initInterstitialAd = function () {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            if (wx.createInterstitialAd) {
                WxUtil_1.interstitialAd = wx.createInterstitialAd({ adUnitId: WxUtil_1.CHAPIN_ID });
                WxUtil_1.interstitialAd.onLoad(function () {
                    console.log('插屏初始化成功');
                });
                WxUtil_1.interstitialAd.onError(function (err) {
                    console.log('interstitialAd onError event emit', err);
                });
                WxUtil_1.interstitialAd.onClose(function (res) {
                    console.log('interstitialAd onClose event emit', res);
                });
            }
        }
    };
    WxUtil.showInterstitialAd = function () {
        if (WxUtil_1.interstitialAd) {
            console.log('显示插屏广告');
            WxUtil_1.interstitialAd.show().catch(function (err) {
                console.error("插屏显示错误", err);
            });
        }
    };
    WxUtil.initGridAd = function () {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            var winSize = wx.getSystemInfoSync();
            if (wx.createGridAd) {
                var gridAd = wx.createGridAd({
                    adUnitId: WxUtil_1.GRID_ID,
                    gridCount: 5,
                    style: {
                        left: (winSize.windowWidth - 300) * 0.5,
                        top: winSize.windowHeight - 80,
                        width: 300,
                        opacity: 0.9
                    }
                });
                gridAd.onError(function (err) {
                    console.log('格子广告初始化错误', err);
                    if (err['errCode'] >= 1000 && err['errCode'] <= 1008) {
                        WxUtil_1.gridAdLoaded = false;
                    }
                });
                WxUtil_1.gridAd = gridAd;
            }
        }
    };
    WxUtil.showGridAd = function () {
        if (WxUtil_1.gridAd) {
            WxUtil_1.gridAd.show().catch(function (err) {
                console.error('格子广告显示错误', err);
            });
        }
    };
    WxUtil.destroyGrid = function () {
        if (WxUtil_1.gridAd != null) {
            WxUtil_1.gridAd.destroy();
            WxUtil_1.gridAd = null;
        }
    };
    WxUtil.showVideo = function () {
        WxUtil_1.videoAd.show()
            .catch(function () {
            WxUtil_1.videoAd.load()
                .then(function () { return WxUtil_1.videoAd.show(); })
                .catch(function (err) {
                console.log('激励视频 广告显示失败', err);
                WxUtil_1.showTip('暂无视频可用！');
                Core_1.default.dispatcher.execute('wxVideoEvent', 0);
            });
        });
    };
    WxUtil.initVideo = function () {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            if (WxUtil_1.videoAd != null) {
                return;
            }
            var videoAd = wx.createRewardedVideoAd({ adUnitId: WxUtil_1.VIDEO_ID });
            WxUtil_1.videoAd = videoAd;
            videoAd.onLoad(function () {
                console.log('激励视频 广告加载成功');
            });
            videoAd.onError(function (err) {
                console.log(err);
                Core_1.default.dispatcher.execute('wxVideoEvent', 0);
            });
            videoAd.onClose(function (res) {
                // 用户点击了【关闭广告】按钮
                // 小于 2.1.0 的基础库版本，res 是一个 undefined
                if (res && res.isEnded || res === undefined) {
                    Core_1.default.dispatcher.execute('wxVideoEvent', 1);
                }
                else {
                    // 播放中途退出，不下发游戏奖励
                    Core_1.default.dispatcher.execute('wxVideoEvent', 0);
                }
            });
        }
    };
    var WxUtil_1;
    WxUtil.bannerAd = null;
    WxUtil.videoAd = null;
    WxUtil.interstitialAd = null;
    WxUtil.gridAd = null;
    //格子广告是否加载成功
    WxUtil.gridAdLoaded = true;
    WxUtil.BANNER_ID = "adunit-6f4bec5e36a78510";
    WxUtil.VIDEO_ID = "adunit-16ecb045e2b0821a";
    WxUtil.CHAPIN_ID = "adunit-a4fce0d262a3324a";
    WxUtil.GRID_ID = "adunit-6bd733d1425b3e23";
    WxUtil = WxUtil_1 = __decorate([
        ccclass
    ], WxUtil);
    return WxUtil;
}(cc.Component));
exports.default = WxUtil;

cc._RF.pop();